<template>
    <div class="con">   
        <img src="../assets/images/logo.png" alt="" class="logo" onclick="return false">    
        <img src="../assets/images/myPoints.png" alt="" onclick="return false">
        <router-link to="/myTicket">
            <img src="../assets/images/btn_MyTicket.png" alt="" class="myticket" >
        </router-link>
        <img src="../assets/images/btn_MyPoints_Active.png" alt="" class="mypoints" onclick="return false">
        <input type="text" readonly :value="point">
        <!-- 积分明细 -->
        <div class="list">
            <ul v-for="item,index in data">
                <li>{{data[index].point}}</li>
                <li>{{data[index].create_time}}</li>
            </ul>
       </div>
        <router-link to="/rankingList">
            <img src="../assets/images/btn_Ranking.png" alt="" class="ranking" >
        </router-link>
        
        <!-- <img src="../assets/images/btn_AddPoints.png" alt="" class="addPoints" @click="scan()"> -->
       
    </div>
</template>

<script>
export default {
    name: "myPoints",
    data() {
        return {
            data: [],
            point: "",
            nowTime: ""
        };
    },
    beforeCreate() {
        document.getElementsByTagName("body")[0].className = "bg_MyPoints";
    },
    methods: {
        scan() {
            wx.scanQRCode({
                desc: "scanQRCode desc",
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: res => {
                    // 回调
                    var result = res.resultStr;
                    var arr = result.split("SCORE");
                    var qs = require("qs");
                    // 请求添加积分接口
                    this.axios({
                        method: "post",
                        url: `${this.siteUrl}/piaowu/api/point_add`,
                        data: qs.stringify({
                            member_id: $.cookie("osc_member_id"),
                            token: $.cookie("osc_member_token"),
                            point: $.trim(arr[1]),
                            code: result
                        })
                    })
                        .then(response => {
                            if (response.data.status == 1) {
                                alert(response.data.data);
                                this.$router.push({
                                    name: "addPoints",
                                    params: {
                                        time: this.nowTime,
                                        point: $.trim(arr[1])
                                    }
                                });
                            } else {
                                alert("请求失败：" + response.data.msg);
                            }
                        })
                        .catch(function(error) {
                            alert(error);
                        });
                },
                error: function(res) {
                    if (res.errMsg.indexOf("function_not_exist") > 0) {
                        alert("版本过低请升级");
                    }
                }
            });
        }
    },
    mounted: function() {
        var qs = require("qs");
        this.axios({
            method: "post",
            url: `${this.siteUrl}/piaowu/api/user_point`,
            data: qs.stringify({
                member_id: $.cookie("osc_member_id"),
                token: $.cookie("osc_member_token")
            })
        })
            .then(response => {
                if (response.data.status == 1) {
                    this.point = response.data.data.point;
                    this.data = response.data.data.history;
                } else {
                    alert("请求失败：" + response.data.msg);
                }
            })
            .catch(function(error) {
                console.log(error);
            });

        var qs1 = require("qs");
        this.axios({
            method: "post",
            url: `${this.siteUrl}/piaowu/api/get_js_sign`,
            data: qs1.stringify({
                url: location.host + location.pathname
            })
        })
            .then(response => {
                wx.config({
                    debug: false,
                    appId: response.data.data.appId,
                    timestamp: response.data.data.timestamp,
                    nonceStr: response.data.data.nonceStr,
                    signature: response.data.data.signature,
                    jsApiList: ["scanQRCode"]
                });
            })
            .catch(err => {
                console.log(err);
            });

        //  获取当前时间
        function p(s) {
            return s < 10 ? "0" + s : s;
        }

        var myDate = new Date();
        //获取当前年
        var year = myDate.getFullYear();
        //获取当前月
        var month = myDate.getMonth() + 1;
        //获取当前日
        var date = myDate.getDate();
        var h = myDate.getHours(); //获取当前小时数(0-23)
        var m = myDate.getMinutes(); //获取当前分钟数(0-59)
        var s = myDate.getSeconds();

        var now =
            year +
            "-" +
            p(month) +
            "-" +
            p(date) +
            " " +
            p(h) +
            ":" +
            p(m) +
            ":" +
            p(s);
        this.nowTime = now;
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    text-align: center;
    position: relative;
}
.con img {
    width: 100%;
}
.con .logo {
    width: 34%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 30%);
}
.con .myticket {
    position: absolute;
    left: 20%;
    top: 19%;
    width: 30%;
}
.con .mypoints {
    position: absolute;
    right: 22%;
    top: 19%;
    width: 30%;
}

.con input {
    position: absolute;
    height: 5%;
    width: 26%;
    right: 24%;
    top: 38%;
    text-align: right;
    border: 1px solid #000;
    padding-right: 2%;
}

.con .ranking {
    position: absolute;
    width: 36%;
    right: 10%;
    bottom: 0%;
}
.con .addPoints {
    position: absolute;
    width: 36%;
    right: 10%;
    bottom: 0%;
}
/* 列表样式 */
.list {
    position: absolute;
    width: 77%;
    height: 19%;
    bottom: 14%;
    left: 10.4%;
}
ul {
    overflow: hidden;
    height: 33%;
}
ul li {
    width: 50%;
    height: 33%;
    float: left;
}
</style>
